package cn.goktech.edu.controller;
import cn.goktech.edu.entity.Teacher;
import cn.goktech.edu.entity.vo.TeacherQuery;
import cn.goktech.edu.service.TeacherService;
import cn.goktech.util.ResultUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author carter
 * @since 2021-01-25
 */

@RestController
@RequestMapping("/edu/teacher")
@Api(description = "讲师管理接口")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    //获取所有讲师信息

    @ApiOperation("查询所有讲师")
    @GetMapping("")
    @CrossOrigin
    public ResultUtil getAllTeacher(){
        List<Teacher> teacherList = teacherService.list(null);
       return  ResultUtil.ok().data("items",teacherList);
    }


    @ApiOperation("根据讲师id获取讲师信息")
    @GetMapping("get/{id}")
    public ResultUtil getTeacherById(@PathVariable(name="id",required = true) String id){
        Teacher byId = teacherService.getById(id);
        return ResultUtil.ok().data("record",byId);
    }

    @ApiOperation("讲师带条件的分页查询")
    @PostMapping("/{page}/{size}") //此处需要前台传JSON数据，需要使用Post提交，否则找不到
    @CrossOrigin
    public ResultUtil getTeacherByPageAndCondition(
            @ApiParam(name = "page",value = "页数") @PathVariable(name = "page",required = true) Long page,
            @ApiParam(name = "size",value = "页数") @PathVariable(name = "size",required = true) Long size,
            @RequestBody TeacherQuery teacherQuery
            ){
        Page<Teacher> pageParam = new Page<>(page,size);
        teacherService.getTeacherPageCondition(pageParam,teacherQuery);

        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return ResultUtil.ok().data("total",total).data("records",records);
    }

    @ApiOperation("根据讲师ID删除讲师")
    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    public ResultUtil deleteTeacher(@ApiParam(name = "id",value = "讲师ID")@PathVariable(name = "id",required = true)String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return ResultUtil.ok();
        }else{
            return ResultUtil.error();
        }
    }

    /*
        LTAI4FzovFeyWfQWujEbpSir
        Vnw914LbsPvpEMwE1K0xAcpZ5gnRr3
     */

    @ApiOperation("讲师更新操作")
    @PutMapping("/update/{id}")
    @CrossOrigin
    public ResultUtil updateTeacher(
            @ApiParam(name = "id",value = "讲师ID") @PathVariable(name = "id",required = true)String id,
            @ApiParam(name = "teacher",value = "修改后讲师对象")@RequestBody Teacher teacher){

        boolean flag = teacherService.updateById(teacher);
        if(flag){
            Teacher resultTeacher = teacherService.getById(teacher.getId());
            return ResultUtil.ok().data("item",resultTeacher);
        }else{
            return ResultUtil.error();
        }
    }

    @ApiOperation("新增讲师接口")
    @CrossOrigin
    @PostMapping("/save")
    public ResultUtil saveTeacher(
           @ApiParam(name="teacher",value = "新增讲师对象") @RequestBody Teacher teacher
            ){
            boolean flag = false;
            flag =  teacherService.save(teacher);
            if(flag){
                return ResultUtil.ok().message("新增成功");
            }else{
                return ResultUtil.error().message("新增失败");
            }
    }
}

